﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Exceptions;
using WmsApp.Export;
using WmsApp.Service;
using WmsApp.Style;
using WmsApp.Util;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormGoodsCodeType : BaseForm
    {
        //const string Condition = " and NewGoodsCode like '%{0}%' and NewGoodsName like '%{1}%'  and OldGoodsCode like '%{2}%' ";
        public FormGoodsCodeType()
        {
            InitializeComponent();
            Lookup.LookupBind.BindingLookUpEditUnit(this.gridViewMain, this.gridColumn7, gridColumn8);
            Lookup.LookupBind.BindingLookUpEditSystemUser(this.gridViewMain, this.gridColumn9, gridColumn10);
            gridViewMain.CustomRowCellEdit += GridViewMainCustomRowCellEdit;
            this.gridControlMainNew.DataSourceChanged +=
                ( o,  args)=> RowStyleConditionController.AddStyleFormatCondition(this.gridViewMain, "Status", "Status='已作废'",Color.Red);

            this.gridControlMainNew.DataSource = new List<GoodsCodeType>();
            //CmdQuery_Click(null, null);
    
        }



        void GridViewMainCustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumn1)
            {
                var goodsCodeType = this.gridViewMain.GetRow(e.RowHandle) as GoodsCodeType;
                if (goodsCodeType != null)
                {
                    if (goodsCodeType.WcfEntityState != EntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                }
            }
        }

        public override string HeadText
        {
            get { return "物资代码管理"; }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var client = EntityServiceFactory.GetEntityService<GoodsCodeType, GoodsCodeTypeService>();

            string condition = " and 1=1 ";

            if (this.comboBoxEditStatus.Text.Trim() == "正常")
            {
                condition += " and ( ExtVarChar3 is null or ExtVarChar3='' or ExtVarChar3='1') ";
            }
            else if (this.comboBoxEditStatus.Text.Trim() == "作废")
            {
                condition += " and  ExtVarChar3 ='0' ";
            }

            if(!String.IsNullOrEmpty(this.textEditNewCode.Text.Trim()))
            {
                condition += (string.Format(" and NewGoodsCode like '%{0}%'  ", this.textEditNewCode.Text.Trim()));
            }

            if (!String.IsNullOrEmpty(this.textEditNewName.Text.Trim()))
            {
                condition += (string.Format(" and NewGoodsName like '%{0}%'  ", this.textEditNewName.Text.Trim()));
            }

            if (!String.IsNullOrEmpty(this.textEditOldCode.Text.Trim()))
            {
                condition += (string.Format(" and OldGoodsCode like '%{0}%'  ", this.textEditOldCode.Text.Trim()));
            }

            var list = client.GetEntityList(condition);
            this.gridControlMainNew.DataSource = list;
          
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            var createGoodsCodeType = GoodsCodeType.CreateGoodsCodeType("请更改代码");
            if (this.gridControlMainNew.DataSource == null)
            {
                this.gridControlMainNew.DataSource = new List<GoodsCodeType>();
            }
            ((List<GoodsCodeType>)this.gridViewMain.DataSource).Add(createGoodsCodeType);
            this.gridControlMainNew.RefreshDataSource();

            for (int idx = 0; idx < ((List<GoodsCodeType>)this.gridViewMain.DataSource).Count; idx++)
            {
                var u = (GoodsCodeType)this.gridViewMain.GetRow(idx);

                if (u == createGoodsCodeType)
                {
                    this.gridViewMain.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                var list = EntityControl.EntityFilterByState.GetNeedSaveEntity(this.gridViewMain.DataSource as List<GoodsCodeType>);
                foreach(var g in list)
                {
                    if(String.IsNullOrEmpty(g.ChargeID) )
                    {
                        XtraMessageBox.Show(g.NewGoodsCode + "：责任人必须输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    if (String.IsNullOrEmpty(g.NewGoodsCode))
                    {
                        XtraMessageBox.Show(g.NewGoodsCode + "：新物料代码必须输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    if (String.IsNullOrEmpty(g.NewGoodsName))
                    {
                        XtraMessageBox.Show(g.NewGoodsCode + "：新物料名称必须输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                }
                EntityServiceFactory.GetEntityService<GoodsCodeType, GoodsCodeTypeService>().SaveEntityList(list);
                XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                var goodsCodeType = this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle) as GoodsCodeType;
                if (goodsCodeType != null)
                {
                    if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show(String.Format("你确实要删除当前记录{0}吗？",goodsCodeType.NewGoodsCode), "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        Factory.WmsServiceFactory.GetServiceClient().DeleteGoodsCodeType(goodsCodeType);
                        ((List<GoodsCodeType>) this.gridControlMainNew.DataSource).Remove(goodsCodeType);
                        this.gridControlMainNew.RefreshDataSource();
                    }
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdImport_Click(object sender, EventArgs e)
        {
            try
            {
                var openFileDialog = new OpenFileDialog();
                openFileDialog.Filter =@"*.xls|*.xls";
                if(System.Windows.Forms.DialogResult.OK == openFileDialog.ShowDialog())
                {
                    //var list = ExcelImportor.ImportGoodsCodeTypeFromExcel(openFileDialog.FileName);
                    var list = ExcelImportor.ImportGoodsCodeTypeFromExcel2(openFileDialog.FileName);
                    if (list.Count > 0)
                    {
                        var dataSourceList = ((List<GoodsCodeType>) this.gridControlMainNew.DataSource);
                        foreach (var goodsCodeType in list)
                        {
                            dataSourceList.Add(goodsCodeType);
                        }
                        gridControlMainNew.RefreshDataSource();
                    }
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                //var goodsCodeType = this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle) as GoodsCodeType;
                //if (goodsCodeType != null)
                //{
                //    if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show(String.Format("你确实要作废当前记录{0}吗？", goodsCodeType.NewGoodsCode), "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                //    {
                //        var obj = Factory.WmsServiceFactory.GetServiceClient().CancelGoodsCodeType(goodsCodeType);
                //        CloneObject.CloneAllPropertyToAnthor<GoodsCodeType>(obj, (GoodsCodeType)this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle));
                //        //((List<GoodsCodeType>)this.gridControlMainNew.DataSource).Remove(goodsCodeType);
                //        this.gridControlMainNew.RefreshDataSource();
                //    }
                //}

                var selectedItems = this.gridViewMain.GetSelectedRows();
                 if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show(String.Format("你确实要作废所选的{0}条物料吗？", selectedItems.Length), "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                 {
                     var client = Factory.WmsServiceFactory.GetServiceClient();
                     foreach (int selectedItem in selectedItems)
                     {
                         var goodsCodeType = this.gridViewMain.GetRow(selectedItem) as GoodsCodeType;
                         if (goodsCodeType != null)
                         {
                             var obj =client.CancelGoodsCodeType(goodsCodeType);
                             CloneObject.CloneAllPropertyToAnthor<GoodsCodeType>(obj, (GoodsCodeType)this.gridViewMain.GetRow(selectedItem));
                             this.gridControlMainNew.RefreshDataSource();
                         }
                     }
                     
                 }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdUnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                var goodsCodeType = this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle) as GoodsCodeType;
                if (goodsCodeType != null)
                {
                    if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show(String.Format("你确实要取消作废当前记录{0}吗？", goodsCodeType.NewGoodsCode), "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        var obj = Factory.WmsServiceFactory.GetServiceClient().UnCancelGoodsCodeType(goodsCodeType);
                        //((List<GoodsCodeType>)this.gridControlMainNew.DataSource).Remove(goodsCodeType);
                        CloneObject.CloneAllPropertyToAnthor<GoodsCodeType>(obj, (GoodsCodeType)this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle));
                        this.gridControlMainNew.RefreshDataSource();
                    }
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.ShowException(exception);
            }
        }
    }
}